# NASA 20111121
# 显示莫尔斯码和明文 并发出电码声音

import pygame
import sys
import time
from test53class_morse2 import Morse


def play_morse(str):
    WIN_X = 400
    WIN_Y = 400

    BLACK = 0, 0, 0
    fps = 3
    fclock = pygame.time.Clock()

    title_name = 'MORSE'
    init_result = pygame.init()

    # Create a window
    screen = pygame.display.set_mode((WIN_X, WIN_Y))
    # Set title
    pygame.display.set_caption(title_name)

    dash_file = 'mp3\\dash.mp3'
    dot_file = 'mp3\\dot.mp3'
    sleeptime = 0.3
    # i = 0
    music_one = True  # 声音只播放一次标记,True:播放 False:不播放
    word='abcdefghijklmnopqrstuvwxyz1234567890()?'
    if list(str)[0]=='.' or list(str)[0]=='-':
        # 如果是莫尔斯码
        morse_word = Morse().morse_to_word(str)
    elif list(str)[0] in word:
        # 如果是明文
        morse_word = str
        str = Morse().word_to_morse(str)

    while True:
        screen.fill(BLACK)
        font = pygame.font.Font(None, 50)
        # 显示莫尔斯码
        morse_text = font.render(str, True, (255, 255, 255))
        # 显示明文
        text = font.render(morse_word, True, (255, 255, 255))
        screen.blit(morse_text, (20, 20))
        screen.blit(text, (20, 80))
        # morse_text2=font.render()
        pygame.display.update()

        if music_one:
            for s in str:
                if s == '-':
                    pygame.mixer.music.load(dash_file)
                    pygame.mixer.music.play()
                    time.sleep(sleeptime + 0.2)
                elif s == '.':
                    pygame.mixer.music.load(dot_file)
                    pygame.mixer.music.play()
                    time.sleep(sleeptime)
                else:
                    time.sleep(sleeptime + 0.4)
                music_one = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                print('BYE-BYE')
                pygame.quit()
                sys.exit()
        # i += 1
        # print(i)
        fclock.tick(fps)
    # # audio_file = 'audios\wrong.m4a'


if __name__ == '__main__':
    word = '一'
    play_morse(word)
